package com.gombe.credit.app.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.gombe.credit.app.GombeCreditApp
import com.gombe.credit.app.R
import com.gombe.credit.app.databinding.BaseGombeCreditViewBinding
import com.gombe.credit.app.utils.GombeCreditTitleBarUtil
import com.gombe.credit.lib.adapter.GombeCreditOnSingleClickListener


/**
 * BaseFragment
 * @Author wwdeng
 * @CreateTime: 2024/11/22 10:32
 * @Annotation：基类 Fragment
 */
abstract class BaseFragment: Fragment(), IBaseView {

    lateinit var rootViewBinding: BaseGombeCreditViewBinding

    /* 是否拦截返回按钮，默认不拦截直接返回 */
    private var isInterceptBack = false

    private var mActivity: Activity? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = activity
    }

    override fun getContext(): Context? {
        return mActivity
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootViewBinding = BaseGombeCreditViewBinding.inflate(inflater)
        rootViewBinding.rootTitleBar.setPadding(0, GombeCreditTitleBarUtil.getStatusBarHeight(),0,0)
        rootViewBinding.rootContainer.addView(takeGombeCreditRootView(inflater), FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        return rootViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (savedInstanceState != null){
            return
        }
        setGombeCreditTitleBarVisibility(View.GONE)
        //返回
        rootViewBinding.rootBack.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                onBackAction()
            }
        })

        initGombeCreditView()
        initGombeCreditListener()
        takeGombeCreditData()
    }

    /**
     * 获取布局文件根视图
     *
     * @return
     */
    protected abstract fun takeGombeCreditRootView(inflater: LayoutInflater): View

    /**
     * 初始化 View 控件
     */
    protected abstract fun initGombeCreditView()

    /**
     * 事件监听
     */
    protected abstract fun initGombeCreditListener()

    /**
     * 加数据
     */
    protected abstract fun takeGombeCreditData()

    /**
     * 设置背景透明
     */
    fun setGombeCreditRootParentTransparent(){
        setGombeCreditRootParentBackgroundColor(getColor(R.color.transparent))
    }

    /**
     * 设置背景颜色
     */
    fun setGombeCreditRootParentBackgroundColor(color: Int){
        rootViewBinding.rootLayout.setBackgroundColor(color)
    }

    /**
     * 设置背景图片
     */
    fun setGombeCreditParentBackgroundResource(drawableId: Int){
        rootViewBinding.rootLayout.setBackgroundResource(drawableId)
    }

    /**
     * @功能描述：是否显示Toolbar
     */
    fun setGombeCreditTitleBarVisibility(visibility: Int) {
        rootViewBinding.rootTitleBar.visibility = visibility
    }

    /**
     * 设置toolBar背景颜色
     */
    fun setGombeCreditTitleBarColor(color: Int) {
        setGombeCreditTitleBarVisibility(View.VISIBLE)
        rootViewBinding.rootTitleBar.setBackgroundColor(color)
    }

    /**
     * 设置toolBar背景图片
     */
    fun setGombeCreditTitleBarRes(res: Int) {
        setGombeCreditTitleBarVisibility(View.VISIBLE)
        rootViewBinding.rootTitleBar.setBackgroundResource(res)
//        rootViewBinding.rootTitleBar.setPadding(0, 0, 0, 0)
    }

    /**
     * 设置是否显示返回
     */
    fun setGombeCreditBackBtnVisibility(visibility: Int) {
        rootViewBinding.rootBack.visibility = visibility
    }

    /**
     * 设置返回按钮图标
     */
    fun setGombeCreditBackBtnRes(res: Int){
        setGombeCreditBackBtnVisibility(View.VISIBLE)
        rootViewBinding.rootBack.setImageResource(res)
    }

    /**
     * 是否显示Title
     */
    fun setGombeCreditTextTitleVisibility(visibility: Int){
        rootViewBinding.rootTitle.visibility = visibility
    }

    /**
     * 设置Title颜色
     */
    fun setGombeCreditTextTitleColor(color: Int) {
        setGombeCreditTextTitleVisibility(View.VISIBLE)
        rootViewBinding.rootTitle.setTextColor(color)
    }

    /**
     * 设置Title
     */
    fun setGombeCreditTextTitle(text: String) {
        setGombeCreditTextTitleVisibility(View.VISIBLE)
        rootViewBinding.rootTitle.text = text
    }

    /**
     * 设置Title对齐方式
     * 默认：Gravity.LEFT
     */
    fun setGombeCreditTextTitleGravity(gravity: Int){
        setGombeCreditTextTitleVisibility(View.VISIBLE)
        rootViewBinding.rootTitle.gravity = gravity
    }

    /**
     * 设置是否显示右侧图标
     */
    fun setGombeCreditRightBtnVisibility(visibility: Int){
        rootViewBinding.rootCustomer.visibility = visibility
    }

    /**
     * 设置右侧按钮图标
     */
    fun setGombeCreditRightBtnRes(res: Int){
        setGombeCreditRightBtnVisibility(View.VISIBLE)
        rootViewBinding.rootCustomer.setImageResource(res)
    }

    override fun showGombeCreditLoading() {
        if (isAdded && !isDetached && !requireActivity().isFinishing){
            val act = context as BaseActivity
            act.showGombeCreditLoading()
        }
    }

    fun showGombeCreditLoading(msg: String="") {
        showGombeCreditLoading(isTOuchOut = false, isCancel = true,msg)
    }

    fun showGombeCreditLoading(isTOuchOut: Boolean, isCancel: Boolean, msg: String="") {
        if (isAdded && !isDetached && !requireActivity().isFinishing){
            val act = context as BaseActivity
            act.showGombeCreditLoading(isTOuchOut,isCancel,msg)
        }
    }

    override fun hideGombeCreditLoading() {
        val act = context as BaseActivity
        act.hideGombeCreditLoading()
    }

    /**
     * toast
     * @param msg
     */
    override fun showGombeCreditToast(msg: String?) {
        val act = context as BaseActivity
        act.showGombeCreditToast(msg)
    }

    /**
     * toast
     * @param msg
     */
    fun showGombeCreditToastDuration(msg: String?, duration: Int) {
        val act = context as BaseActivity
        act.showGombeCreditToastDuration(msg, duration)
    }

    /**
     * 页面跳转（不带参数）
     *
     * @param T
     */
    open fun onIntent(T: Class<*>?) {
        val act = context as BaseActivity
        act.onIntent(T)
    }

    /**
     * 页面跳转（带参数）
     *
     * @param T
     * @param bundle
     */
    open fun onIntent(T: Class<*>?, bundle: Bundle?) {
        val act = context as BaseActivity
        act.onIntent(T,bundle)
    }

    /* 隐藏键盘 */
    fun onHideSoftInput(v: View) {
        val act = context as BaseActivity
        act.onHideSoftInput(v)
    }

    /* 显示键盘 */
    fun onShowSoftInput(v: View) {
        val act = context as BaseActivity
        act.onShowSoftInput(v)
    }

    fun getColor(color: Int): Int{
        return GombeCreditApp.app.getColor(color)
    }

    /* 返回 */
    fun onBackAction() {
        if (!isInterceptBack) { //不拦截，直接返回
            mActivity?.finish()
        }
    }

    /* 是否拦截返回按钮 */
    fun isInterceptBack(isBackExit: Boolean) {
        isInterceptBack = isBackExit
    }

}
